package br.pucrs.lognplay.service;

import java.util.List;

import javax.ejb.Stateless;

import br.pucrs.lognplay.entity.DynamicView;
import br.pucrs.lognplay.repository.DynamicViewRepository;
import br.pucrs.lognplay.repository.RepositoryManager;

@Stateless
public class DynamicViewManagementServiceBean implements DynamicViewManagementService {

	public void mergeDynamicView(DynamicView dynamicView) throws ServiceException {
		try {
			RepositoryManager.beginTransaction();
			DynamicViewRepository dynamicViewRepository = RepositoryManager.getDynamicViewRepository();
			dynamicViewRepository.merge(dynamicView);
			RepositoryManager.commitTransaction();
		} catch (Exception e) {
			RepositoryManager.rollbackTransaction();
			throw new ServiceException("Error on merge a dynamic view", e);
		}
	}

	public DynamicView getDynamicViewById(String id) throws ServiceException {
		try {
			RepositoryManager.beginTransaction();
			DynamicViewRepository dynamicViewRepository = RepositoryManager.getDynamicViewRepository();
			DynamicView dynamicView = dynamicViewRepository.get(id);
			RepositoryManager.commitTransaction();
			return dynamicView;
		} catch (Exception e) {
			RepositoryManager.rollbackTransaction();
			throw new ServiceException("Error on get a dynamic view by id", e);
		}
	}

	public List<DynamicView> listAllDynamicViews() throws ServiceException {
		try {
			RepositoryManager.beginTransaction();
			DynamicViewRepository dynamicViewRepository = RepositoryManager.getDynamicViewRepository();
			List<DynamicView> dynamicViews = dynamicViewRepository.listAll();
			RepositoryManager.commitTransaction();
			return dynamicViews;
		} catch (Exception e) {
			RepositoryManager.rollbackTransaction();
			throw new ServiceException("Error on list all dynamic views", e);
		}
	}
}
